class Store {
  _store: Storage;
  constructor(type: 'localStorage' | 'sessionStorage') {
    this._store = window[type];
  }
  set(key, val) {
    this._store.setItem(key, this.toJsonStr(val));
  }
  get(key) {
    let ret = this._store.getItem(key);
    try {
      ret = JSON.parse(ret);
    } catch (err) {}
    return ret;
  }
  del(key) {
    this._store.removeItem(key)
  }
  clear() {
    this._store.clear()
  }
  toJsonStr(val) {
    const isString = typeof val === 'string';
    if (isString) {
      return val;
    } else {
      return JSON.stringify(val);
    }
  }
}

export const SESSION = new Store('sessionStorage');
export const LOCAL = new Store('localStorage');
